//
//  DataTableViewCell.m
//  ZYZB
//
//  Created by ENERGY on 2018/6/25.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "DataTableViewCell.h"

@interface DataTableViewCell()
@property (nonatomic, strong)UILabel * titleLab;//平台数据标题
@property (nonatomic, strong)UIView * leftView;//左视图
@property (nonatomic, strong)UIView * middleView;//中视图
@property (nonatomic, strong)UIView * rightView;//右视图
@property (nonatomic, strong)UILabel * totalBidLab;//累计投标
@property (nonatomic, strong)UILabel * totalEarningsLab;//累计收益
@property (nonatomic, strong)UILabel * totalUserLab;//累计用户
@property (nonatomic, strong)UILabel * bidTitleLab;//累计投标标题
@property (nonatomic, strong)UILabel * earningsTitleLab;//累计收益标题
@property (nonatomic, strong)UILabel * userTitleLab;//累计用户标题
@property (nonatomic, strong)UILabel * runTimeLab;//运营时间

@end
@implementation DataTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        __weak typeof(self) weakSelf = self;
        UIView * topLineView = [[UIView alloc] init];
        topLineView.backgroundColor = getColor(grayColor);
//        topLineView.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:topLineView];
        [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weakSelf);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        UIView * bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = getColor(grayColor);
//        bottomLineView.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:bottomLineView];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(weakSelf);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        [self.contentView addSubview:self.titleLab];
        [self.contentView addSubview:self.leftView];
        [self.contentView addSubview:self.middleView];
        [self.contentView addSubview:self.rightView];
        [self.contentView addSubview:self.totalBidLab];
        [self.contentView addSubview:self.totalEarningsLab];
        [self.contentView addSubview:self.totalUserLab];
        [self.contentView addSubview:self.bidTitleLab];
        [self.contentView addSubview:self.earningsTitleLab];
        [self.contentView addSubview:self.userTitleLab];
        [self.contentView addSubview:self.runTimeLab];
        int viewW = (SCREEN_WIDTH - (22/WIDTH_6_SCALE + 36/WIDTH_6_SCALE))/3;
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(18/WIDTH_6_SCALE);
            make.top.mas_equalTo(16/WIDTH_6_SCALE);
            make.height.mas_equalTo(13/WIDTH_6_SCALE);
        }];
        [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(18/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.titleLab.mas_bottom).mas_offset(14/WIDTH_6_SCALE);
            make.width.mas_equalTo(viewW);
            make.height.mas_equalTo(60/WIDTH_6_SCALE);
        }];
        [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.leftView.mas_right).mas_offset(11/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.rightView.mas_left).mas_offset(-11/WIDTH_6_SCALE);
            make.top.bottom.equalTo(weakSelf.leftView);
        }];
        [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.height.equalTo(weakSelf.leftView);
            make.right.equalTo(weakSelf.mas_right).mas_offset(-18/WIDTH_6_SCALE);
            make.width.equalTo(weakSelf.leftView);
        }];
        [self.totalBidLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.leftView);
            make.top.equalTo(weakSelf.leftView.mas_top).mas_offset(20/WIDTH_6_SCALE);
            make.height.mas_equalTo(10/WIDTH_6_SCALE);
        }];
        [self.totalEarningsLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.middleView);
            make.height.top.equalTo(weakSelf.totalBidLab);
        }];
        [self.totalUserLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.rightView);
            make.height.top.equalTo(weakSelf.totalBidLab);
        }];
        [self.bidTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.totalBidLab.mas_bottom).mas_offset(5/WIDTH_6_SCALE);
            make.left.right.equalTo(weakSelf.leftView);
            make.height.mas_equalTo(10/WIDTH_6_SCALE);
        }];
        [self.earningsTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.bidTitleLab);
            make.left.right.equalTo(weakSelf.middleView);
            make.height.equalTo(weakSelf.bidTitleLab);
        }];
        [self.userTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.bidTitleLab);
            make.left.right.equalTo(weakSelf.rightView);
            make.height.equalTo(weakSelf.bidTitleLab);
        }];
        [self.runTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.middleView.mas_bottom).mas_equalTo(10/WIDTH_6_SCALE);
            make.left.right.equalTo(weakSelf.contentView);
            make.height.mas_equalTo(12/WIDTH_6_SCALE);
        }];
        
    }return self;
}
#pragma mark - init
- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = DEF_FontSize_14;
        _titleLab.textColor = [UIColor blackColor];
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.text = @"平台数据";
    }return _titleLab;
}
- (UIView *)leftView{
    if (!_leftView) {
        _leftView = [[UIView alloc] init];
        _leftView.backgroundColor = [UIColor whiteColor];
        _leftView.layer.borderColor = getColor(@"bdbdbd").CGColor;
        _leftView.layer.borderWidth = 1/WIDTH_6_SCALE;
        _leftView.layer.cornerRadius = 4;
        _leftView.layer.masksToBounds = YES;
    }return _leftView;
}
- (UIView *)middleView{
    if (!_middleView) {
        _middleView = [[UIView alloc] init];
        _middleView.backgroundColor = [UIColor whiteColor];
        _middleView.layer.borderColor = getColor(@"bdbdbd").CGColor;
        _middleView.layer.borderWidth = 1/WIDTH_6_SCALE;
        _middleView.layer.cornerRadius = 4;
        _middleView.layer.masksToBounds = YES;
    }return _middleView;
}
- (UIView *)rightView{
    if (!_rightView) {
        _rightView = [[UIView alloc] init];
        _rightView.backgroundColor = [UIColor whiteColor];
        _rightView.layer.borderColor = getColor(@"bdbdbd").CGColor;
        _rightView.layer.borderWidth = 1/WIDTH_6_SCALE;
        _rightView.layer.cornerRadius = 4;
        _rightView.layer.masksToBounds = YES;
    }return _rightView;
}
- (UILabel *)totalBidLab{
    if (!_totalBidLab) {
        _totalBidLab = [[UILabel alloc] init];
        _totalBidLab.font = DEF_FontSize_12;
        _totalBidLab.textColor = [UIColor blackColor];
        _totalBidLab.textAlignment = NSTextAlignmentCenter;
    }return _totalBidLab;
}
- (UILabel *)totalEarningsLab{
    if (!_totalEarningsLab) {
        _totalEarningsLab = [[UILabel alloc] init];
        _totalEarningsLab.font = DEF_FontSize_12;
        _totalEarningsLab.textColor = [UIColor blackColor];
        _totalEarningsLab.textAlignment = NSTextAlignmentCenter;
    }return _totalEarningsLab;
}
- (UILabel *)totalUserLab{
    if (!_totalUserLab) {
        _totalUserLab = [[UILabel alloc] init];
        _totalUserLab.font = DEF_FontSize_12;
        _totalUserLab.textColor = [UIColor blackColor];
        _totalUserLab.textAlignment = NSTextAlignmentCenter;
    }return _totalUserLab;
}
- (UILabel *)bidTitleLab{
    if (!_bidTitleLab) {
        _bidTitleLab = [[UILabel alloc] init];
        _bidTitleLab.font = DEF_FontSize_10;
        _bidTitleLab.textColor = getColor(@"bdbdbd");
        _bidTitleLab.textAlignment = NSTextAlignmentCenter;
        _bidTitleLab.text = @"累计投标金额";
    }return _bidTitleLab;
}
- (UILabel *)earningsTitleLab{
    if (!_earningsTitleLab) {
        _earningsTitleLab = [[UILabel alloc] init];
        _earningsTitleLab.font = DEF_FontSize_10;
        _earningsTitleLab.textColor = getColor(@"bdbdbd");
        _earningsTitleLab.textAlignment = NSTextAlignmentCenter;
        _earningsTitleLab.text = @"累计用户收益";
    }return _earningsTitleLab;
}
- (UILabel *)userTitleLab{
    if (!_userTitleLab) {
        _userTitleLab = [[UILabel alloc] init];
        _userTitleLab.font = DEF_FontSize_10;
        _userTitleLab.textColor = getColor(@"bdbdbd");
        _userTitleLab.textAlignment = NSTextAlignmentCenter;
        _userTitleLab.text = @"累计平台用户";
    }return _userTitleLab;
}
- (UILabel *)runTimeLab{
    if (!_runTimeLab) {
        _runTimeLab = [[UILabel alloc] init];
        _runTimeLab.font = DEF_FontSize_14;
        _runTimeLab.textColor = getColor(@"bdbdbd");
        _runTimeLab.textAlignment = NSTextAlignmentCenter;
        _runTimeLab.text = @"稳定运营时间 888天";
    }return _runTimeLab;
}
#pragma mark - model
- (void)setModelData:(NSDictionary *)modelData{
    _modelData = modelData;
    //累计投标金额
    NSString * totalBidStr = [NSString stringWithFormat:@"%@亿元", modelData[@"lend"]];
    [self changeStringTypeface:totalBidStr withFirstLoc:0 withSecondLocStr:@"亿" withLabel:self.totalBidLab];
    //累计用户收益
    NSString * totalEarningsStr = [NSString stringWithFormat:@"%@万元", modelData[@"profit"]];
    [self changeStringTypeface:totalEarningsStr withFirstLoc:0 withSecondLocStr:@"万元" withLabel:self.totalEarningsLab];
    //累计平台用户
    NSString * totalUserStr = [NSString stringWithFormat:@"%@人", modelData[@"amount"]];
    [self changeStringTypeface:totalUserStr withFirstLoc:0 withSecondLocStr:@"人" withLabel:self.totalUserLab];
    self.runTimeLab.text = [NSString stringWithFormat:@"稳定运营时间 %@天", modelData[@"time"]];
}
#pragma mark - 方法
//首字母就是数字的
- (void)changeStringTypeface:(NSString *)string withFirstLoc:(NSInteger)firstLoc withSecondLocStr:(NSString *)secondLocStr withLabel:(UILabel *)label{
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:string];
//     需要改变的最后一个文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:secondLocStr].location;
    // 需要改变的区间
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:range];
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18] range:range];
    [label setAttributedText:noteStr];
}

@end
